/**
 * Created by Demon-pro on 2015/6/28.
 */
var Choose = function () {
    function popoverHandle() {
        var data = [
            {
                name: "钢球磨煤机",
                data: [
                    {
                        type: "贮仓式",
                        K: "0.2~0.4*"
                    },
                    {
                        type: "直吹式",
                        K: "0.25"
                    }
                ]
            },
            {
                name: "中速磨煤机",
                data: [
                    {
                        type: "负压",
                        K: "0.2"
                    }
                ]
            },
            {
                name: "风扇磨煤机",
                data: [
                    {
                        type: "不带烟气下降管",
                        K: "0.2"
                    },
                    {
                        type: "带烟气下降管",
                        K: "0.3"
                    }
                ]
            }
        ];

        function getTable() {
            var $table = $("<table>").css("width", "600px").addClass("table table-bordered").append("<thead>").append("<tbody>").append("<tfoot>");
            var $thead = $table.children("thead");
            var $tbody = $table.children("tbody");
            var $tfoot = $table.children("tfoot");

            (function creHead() {
                $thead.append("<tr>");
                $thead.children("tr").append("<th>");

                for (var i = 0, l = data.length; i < l; i++) {
                    $("<th>").attr({
                        "colspan": data[i].data.length
                    }).addClass("text-center")
                        .text(data[i].name).appendTo($thead.children("tr"));
                }
            })();

            (function creBody() {
                $row1 = $("<tr>");
                $("<td>").addClass("text-center").text("制粉系统型式").appendTo($row1);

                $row2 = $("<tr>");

                $("<td>").addClass("text-center").html("漏风系数,K<sub>le</sub>").appendTo($row2);

                for (var i = 0, l = data.length; i < l; i++) {
                    for (var j = 0; j < data[i].data.length; j++) {
                        $("<td>").addClass("text-center").text(data[i].data[j].type).appendTo($row1);
                        $("<td>").addClass("text-center").text(data[i].data[j].K).appendTo($row2);
                    }
                }

                $tbody.append($row1).append($row2);
            })();

            (function creFoot() {
                var colspan = $tbody.find("tr:eq(0) > td").size();
                $tfoot.append("<tr>");
                $("<td>").attr({"colspan": colspan}).addClass("text-center")
                    .text("* 可按磨煤机直径D取值; D≥2m 取 0.4; D≥3m 取 0.3; D≥4m 取 0.2")
                    .appendTo($tfoot.children('tr'));
            })();

            return $("<div>").addClass("col-md-12").append($table).html();
        }

        $("#E61").popover({
            html: true,
            placement: "top",
            trigger: "focus",
            content: function () {
                return '<div class="col-md-12">' + getTable() + '</div>';
            }()
        })
    }

    function splitterHandle() {
        function getVal() {
            switch ($("#splitter_type").val()) {
                case "static":
                    $("#E2").val("1.0");
                    break;
                case "dynamic":
                    $("#E2").val("1.2");
                    break;
            }
        }

        $("#splitter_type").on("change", function () {
            getVal();
            Choose.updata("E2");
        });
        if (!$.isNumeric($("#E2").val()))
        getVal();
    }

    function buttonHandle() {
        $("#nextPage").on("click", function () {
            nextPage("mill/calculate?menuId=37",11);
        })
    }

    function calculateInit() {
        $.ajax({
            url: basePath + "mill/select/getCalculateInit",
            dataType: "json",
            type: "post"
        })
    }

    var save = function (exParam, exVal) {
        if (validate.isNotNumber(exVal)) {
            validate.invalid();
            return;
        }
        $.ajax({
            url: basePath + "mill/select/saveValueOfMillSelect",
            dataType: "json",
            type: "post",
            //放了三个值，一个填到默认Ex当中，其他两个分别对应属性值和属性名
            data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
        });
        validate.valid();
    };



    return {
        init: function () {
            popoverHandle();
            splitterHandle();
            buttonHandle();
            calculateInit();
        },
        updata: function (ExParam) {
            //定义保存数据方法
            var $obj = $("#" + ExParam);
            var ExVal = $obj.val();
            validate.init($obj);
            if (ExVal != "") {
                save(ExParam, ExVal);
            }
        },
        cal:function (ExParam){
            $.ajax({
                url: basePath + "admin/formal/calculate",
                dataType: "json",
                type: "post",
                //燃烧前缀 R
                data: "paramName=A" + ExParam,
                success: function (data) {
                    $("#"+ExParam).val(data);
                    save(ExParam, data);
                }
            });
        }
    }
}();